/**
 * 
 */
package com.glm.wwii.battleships.gamelogic;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import com.glm.wwii.battleships.defence.Const.ConstValue;
import android.content.Context;
import android.util.Log;

/**
 * @author utente
 *
 */
public class ResourceLoader {
	
	private Context mContext;
	private BaseGameActivity mActivity;
	private Hashtable<String, ITexture> mTextureTable=new Hashtable<String, ITexture>();
	
	/**
	 * 
	 */
	public ResourceLoader(Context oContext,BaseGameActivity oActivity) {
		mContext=oContext;
		mActivity=oActivity;
	}
	/**
	 * Carica tutte le risorse in un HashTable 
	 * per poi essere caricate dal WWII
	 * 
	 * */
	public Hashtable<String, ITexture> loadGameTexture(Context oContext,BaseGameActivity oActivity){
		try {		    
			mContext=oContext;
			mActivity=oActivity;
			ITexture tLevel1Texture = new BitmapTexture(this.mActivity.getTextureManager(), new IInputStreamOpener() {
		        public InputStream open() throws IOException {
		            return mContext.getAssets().open(ConstValue.S_LEVEL_1_GAME_TEXTURE);
		        }
		    });
			mTextureTable.put("background", tLevel1Texture);
			
			ITexture tCorvetTexture = new BitmapTexture(this.mActivity.getTextureManager(), new IInputStreamOpener() {
		        public InputStream open() throws IOException {
		            return mContext.getAssets().open(ConstValue.S_ENEMY_CORVET_TEXTURE);
		        }
		    });
		    		    
		    mTextureTable.put("corvet", tCorvetTexture);
			
			
			ITexture tDestroyerTexture = new BitmapTexture(this.mActivity.getTextureManager(), new IInputStreamOpener() {
		        public InputStream open() throws IOException {
		            return mContext.getAssets().open(ConstValue.S_ENEMY_DESTROYER_TEXTURE);
		        }
		    });
		    		    
		    mTextureTable.put("destroyer", tDestroyerTexture);
		    
		} catch (IOException e1) {
		    Log.e(this.getClass().getCanonicalName(),e1.getMessage());
		}				
		return mTextureTable;		
	}
	/**
	 * Carica tutte le risorse in un HashTable 
	 * per poi essere caricate dal WWII
	 * 
	 * */
	public TextureRegion loadSplashTexture(Context oContext,BaseGameActivity oActivity){
		try {
			mContext=oContext;
			mActivity=oActivity;
		    // 1 - Set up bitmap textures
		    ITexture mSplashBackgroundTexture = new BitmapTexture(this.mActivity.getTextureManager(), new IInputStreamOpener() {		     
		        public InputStream open() throws IOException {
		            return ResourceLoader.this.mContext.getAssets().open(ConstValue.S_SPLASH_TEXTURE);
		        }
		    });		    	
		    mSplashBackgroundTexture.load();
		    return TextureRegionFactory.extractFromTexture(mSplashBackgroundTexture);		  
		} catch (IOException e1) {
		    Log.e(this.getClass().getCanonicalName(),e1.getMessage());
		    return null;
		}
	}
	/**
	 * Carica tutte le risorse in un HashTable 
	 * per poi essere caricate dal WWII
	 * 
	 * */
	public TextureRegion loadMainTexture(Context oContext,BaseGameActivity oActivity){
		try {
			mContext=oContext;
			mActivity=oActivity;
		    // 1 - Set up bitmap textures
		    ITexture mSplashBackgroundTexture = new BitmapTexture(this.mActivity.getTextureManager(), new IInputStreamOpener() {		     
		        public InputStream open() throws IOException {
		            return ResourceLoader.this.mContext.getAssets().open(ConstValue.S_MAIN_TEXTURE);
		        }
		    });		    	
		    mSplashBackgroundTexture.load();
		    return TextureRegionFactory.extractFromTexture(mSplashBackgroundTexture);		  
		} catch (IOException e1) {
		    Log.e(this.getClass().getCanonicalName(),e1.getMessage());
		    return null;
		}
	}
	/**
	 * Carica tutte le risorse in un HashTable 
	 * per poi essere caricate dal WWII
	 * 
	 * */
	public Hashtable<String, TiledTextureRegion> loadMainTexture(Context oContext,BaseGameActivity oActivity,Engine mEngine){		
		mContext=oContext;
		mActivity=oActivity;
			Hashtable<String, TiledTextureRegion> mTextureRegionTable=new Hashtable<String, TiledTextureRegion>();
			TiledTextureRegion mStartTextureRegion;
			TiledTextureRegion mOptionsTextureRegion;
			TiledTextureRegion mLevelTextureRegion;
			TiledTextureRegion mWallTextureRegion;
			//TiledTextureRegion mMain;
			
		    
			BitmapTextureAtlas mBtnStartBitmapTextureAtlas = new BitmapTextureAtlas(this.mActivity.getTextureManager(), 302, 224, TextureOptions.BILINEAR);
			BitmapTextureAtlas mBtnOptionsBitmapTextureAtlas = new BitmapTextureAtlas(this.mActivity.getTextureManager(), 302, 224, TextureOptions.BILINEAR);
			BitmapTextureAtlas mBtnLevelBitmapTextureAtlas = new BitmapTextureAtlas(this.mActivity.getTextureManager(), 302, 224, TextureOptions.BILINEAR);
			BitmapTextureAtlas mBtnWallBitmapTextureAtlas = new BitmapTextureAtlas(this.mActivity.getTextureManager(), 302, 224, TextureOptions.BILINEAR);
			//BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(pBitmapTextureAtlas, pAssetManager, pAssetPath, pTextureX, pTextureY, pTileColumns, pTileRows);
			mStartTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBtnStartBitmapTextureAtlas, this.mActivity.getAssets(), ConstValue.S_BTN_START_TEXTURE, 0,0, 1, 2);
			mOptionsTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBtnOptionsBitmapTextureAtlas, this.mActivity.getAssets(), ConstValue.S_BTN_OPTIONS_TEXTURE,0,0, 1, 2);
			mLevelTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBtnLevelBitmapTextureAtlas, this.mActivity.getAssets(), ConstValue.S_BTN_EXIT_TEXTURE, 0,0,1, 2);
			mWallTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBtnWallBitmapTextureAtlas, this.mActivity.getAssets(), ConstValue.S_BTN_ABOUT_TEXTURE,0,0, 1, 2);
			
			//mMain = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this.mActivity.getAssets(), ConstValue.S_MAIN_TEXTURE,0,0, 1, 1);			     
			mEngine.getTextureManager().loadTexture(mBtnStartBitmapTextureAtlas);
			mEngine.getTextureManager().loadTexture(mBtnOptionsBitmapTextureAtlas);
			mEngine.getTextureManager().loadTexture(mBtnLevelBitmapTextureAtlas);
			mEngine.getTextureManager().loadTexture(mBtnWallBitmapTextureAtlas);
		   		    
			//mTextureRegionTable.put("main", mMain);
		    mTextureRegionTable.put("btn_start", mStartTextureRegion);
		    mTextureRegionTable.put("btn_options", mOptionsTextureRegion);
		    mTextureRegionTable.put("btn_exit", mLevelTextureRegion);
		    mTextureRegionTable.put("btn_about", mWallTextureRegion);
		    return mTextureRegionTable;		  		
	}
	/**
	 * Inizializza gli effetti sonori disponibili
	 * 
	 * @param mEngine
	 */
	public Hashtable<String, Sound> loadMainSoundFX(Context oContext, Engine mEngine) {
		Hashtable<String, Sound> mSoundTable=new Hashtable<String, Sound>();
		mContext=oContext;
		SoundFactory.setAssetBasePath(ConstValue.S_BASIC_SOUND_FOLDER);				
		try {
			mSoundTable.put("click", SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), mContext, "click.ogg"));			
		} catch (IllegalStateException e) {
			Log.e(this.getClass().getCanonicalName(),"Error IllegalStateException when load Sound");
		} catch (IOException e) {
			Log.e(this.getClass().getCanonicalName(),"Error IOException when load Sound");
		}
		return mSoundTable;
	}
	/**
	 * Inizializza le musiche di sottofondo
	 * 
	 * @param mEngine
	 */
	public Hashtable<String, Music> loadMainMusicFX(Context oContext, Engine mEngine) {
		Hashtable<String, Music> mMusicTable=new Hashtable<String, Music>();
		mContext=oContext;
		MusicFactory.setAssetBasePath(ConstValue.S_BASIC_MUSIC_FOLDER);				
		try {
			mMusicTable.put("main", MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), mContext, "main.ogg"));			
		} catch (IllegalStateException e) {
			Log.e(this.getClass().getCanonicalName(),"Error IllegalStateException when load Sound");
		} catch (IOException e) {
			Log.e(this.getClass().getCanonicalName(),"Error IOException when load Sound");
		}
		return mMusicTable;
	}
}
